/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.service.MedicalHistoryService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class AddMedicalHistory extends GenericAutowireComposer {

    private Label patientName;
    private Textbox result;
    private Textbox medicineRecipe;
    private Intbox height;
    private Intbox weight;
    private Intbox vessel;
    private Window self;
    private Patient patient;
    private MedicalHistoryService medicalHistoryService = util.Utility.getUtility().getMedicalHistoryService();

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        patient = (Patient) arg.get("patient");
        patientName.setValue(patient.getFullName());
    }

    public void onReset(Event e) {
        result.setValue("");
        medicineRecipe.setValue("");
        height.setValue(0);
        weight.setValue(0);
        vessel.setValue(0);
    }

    public void onClose(Event e) {
        self.onClose();
    }

    public void onSubmit(Event e) {
        try {
            medicalHistoryService.createMedicalHistory(patient.getPatientId(),result.getValue(),
                    medicineRecipe.getValue(),weight.getValue(), height.getValue(),vessel.getValue());
            Messagebox.show("New medical history of patient "+patient.getFullName()+" has been inserted!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            self.onClose();
            Executions.sendRedirect("ViewMedicalHistories.do?patientId="+patient.getPatientId());
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
